const simpleGit = require("simple-git")
const fs = require('fs')

const datas = []
for(var i = 0; i < 10000; ++i) {
    const filePath = `./data/${i + 1}.json`
    if (!fs.existsSync(filePath)) break;
    datas.push(require(filePath))
}

function getRepoName(url) {
    const pos = url.lastIndexOf('/')
    const dotPos = url.lastIndexOf('.')

    return url.substring(pos + 1, dotPos)
}

// https://api.github.com/orgs/kanripo/repos?per_page=100000&page=1

let index = 0;
let j = 0;

try {
    const lastItem = require('./curr_clone.json')
    if (lastItem) {
        index = lastItem.index
        j = lastItem.j
    }
} catch(e) {

}


function clone() {
    try {
        console.log(`..cloning:${index}-${j}`)

        var gitItem = datas[index][j]

        fs.writeFileSync('./curr_clone.json', JSON.stringify({
            index: index,
            j: j,
            url: gitItem.clone_url
        }))
        
        const git = simpleGit()
        var localPath = './han-book/' + getRepoName(gitItem.clone_url);
        if (fs.existsSync(localPath)) {
            ++j;
            if (j >= datas[index].length) {
                j = 0;
                ++index;
            }

            console.log('..already done, continue')

            if (index < datas.length) {
                clone()
                // console.log('..done')
            }
            else {
                console.log('..done')
            }

            return;
        }
        git.clone(gitItem.clone_url, localPath, null, (err)=>{
            if (!err) {
                ++j;
                if (j >= datas[index].length) {
                    j = 0;
                    ++index;
                }

                if (index < datas.length) {
                    clone()
                    // console.log('..done')
                }
                else {
                    console.log('..done')

                    var dirs = fs.readdirSync('./han-book')
                    console.log('...dirs', dirs.length)
                }
            } else {
                console.log('...error', err)

                console.log('..err, 5s later to continue')
                setTimeout(clone, 5000);
            }
        })
    }
    catch(e) {
        console.log('..exception, 5s later to continue')
        setTimeout(clone, 5000);
    }
}

clone()